package com.madarasan.xml;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Message implements Comparable<Message>, Parcelable {
	public Message(String title, String link, String description, String tempMax,
			String tempMin, String wind, String snow, String weather, String date) {
		super();
		this.title = title;
		this.link = link;
		this.description = description;
		this.tempMax = tempMax;
		this.tempMin = tempMin;
		this.wind = wind;
		this.snow = snow;
		this.weather = weather;
		this.date = date;
	}

	public Message() {
		// TODO Auto-generated constructor stub
	}
	
	public Message(Parcel in) {
		// TODO Auto-generated constructor stub
	}

	

	static SimpleDateFormat FORMATTER = 
		new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
	private String title;
	private String link;
	private String description;
	private String tempMax;
	private String tempMin;
	private String wind;
	private String snow;
	private String weather;
	private String date;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title.trim();
	}
	// getters and setters omitted for brevity 
	public String getLink() {
		return link;
	}
	
	public void setLink(String link) {
		/*try {
			this.link = new URL(link);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}*/
		this.link = link;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description.trim();
	}
	
	public String getTempMax() {
		return tempMax;
	}

	public void setTempMax(String tempMax) {
		this.tempMax = tempMax;
	}

	public String getTempMin() {
		return tempMin;
	}

	public void setTempMin(String tempMin) {
		this.tempMin = tempMin;
	}

	public String getWind() {
		return wind;
	}

	public void setWind(String wind) {
		this.wind = wind;
	}

	public String getSnow() {
		return snow;
	}

	public void setSnow(String snow) {
		this.snow = snow;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public String getDate() {
		//return FORMATTER.format(this.date);
		return date;
	}

	public void setDate(String date) {
		/*// pad the date if necessary
		while (!date.endsWith("00")){
			date += "0";
		}
		try {
			this.date = FORMATTER.parse(date.trim());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}*/
		this.date = date;
	}
	
	public Message copy(){
		Message copy = new Message();
		copy.title = title;
		copy.link = link;
		copy.description = description;
		copy.tempMax = tempMax;
		copy.tempMin = tempMin;
		copy.wind = wind;
		copy.snow = snow;
		copy.weather = weather;
		copy.date = date;
		return copy;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Title: ");
		sb.append(title);
		sb.append('\n');
		sb.append("Date: ");
		sb.append(this.getDate());
		sb.append('\n');
		sb.append("Link: ");
		sb.append(link);
		sb.append('\n');
		sb.append("Description: ");
		sb.append(description);
		sb.append("Maximum temp: ");
		sb.append(tempMax);
		sb.append("minimum temp: ");
		sb.append(tempMin);
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (link == null) {
			if (other.link != null)
				return false;
		} else if (!link.equals(other.link))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	public int compareTo(Message another) {
		if (another == null) return 1;
		// sort descending, most recent first
		return another.date.compareTo(date);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void readFromParcel(Parcel in) {
		title = in.readString();
		
		
	}
	
	
	@Override
	public void writeToParcel(Parcel arg0, int arg1) {
		Log.v("Parcels", "writeToParcel..."+ arg1);
	
		arg0.writeString(title);
		arg0.writeString(link);
		arg0.writeString(description);
		arg0.writeString(tempMax);
		arg0.writeString(tempMin);
		arg0.writeString(wind);
		arg0.writeString(snow);
		arg0.writeString(weather);
		arg0.writeString(date);
	      
	

		
	}
	
	
	public static final Parcelable.Creator<Message> CREATOR
    = new Parcelable.Creator<Message>() {
		public Message createFromParcel(Parcel in) {
		return new Message(in);
			}

		public Message[] newArray(int size) {
    return new Message[size];
		}
	};

	

	
}
